package com.app.tools;

import lombok.Data;

import java.util.List;

@Data
public class PageInfo<T>{
    private int pageNum;//当前页
    private int pageSize=5;//一页多少行
    private int totalPages;//总页数
    private int totalCount;//总数据条数
    private int upPage;//前一页
    private int nextPage;//后一页
    private int[] numb;//页码
    private List<T> list;//数据

    public PageInfo() {
    }

    public PageInfo(int pageNum,List<T> list) {
        this.pageNum = pageNum;
        this.totalCount = list.size();
        this.totalPages = totalCount%pageSize==0?
                totalCount/pageSize:totalCount/pageSize+1;
        updateUpNext(pageNum);
        updateNumb();
        upList(pageNum,list);
    }

    public void updateNumb(){
        if(totalPages<3){
            numb=totalPages<2?new int[]{1}:new int[]{1,2};
        }else{
            if(upPage<1){
                numb=new int[]{1,2,3};
            }else if(nextPage>totalPages){
                numb=new int[]{upPage-1,upPage,pageNum};
            }else{
                numb=new int[]{upPage,pageNum,nextPage};
            }
        }
    }

    public void updateUpNext(int pageNum){
        this.upPage=pageNum-1;
        this.nextPage=pageNum+1;
    }

    public void upList(int pageNum,List<T> list){
        if(totalCount<=pageSize){
            this.list=list.subList(0,totalCount);
        }else {
            if(pageNum==1){
                this.list=list.subList(0,pageSize);
            }else if(pageNum*pageSize>=totalCount){
                this.list=list.subList((pageNum-1)*pageSize,totalCount);
            }else{
                this.list=list.subList((pageNum-1)*pageSize,pageNum*pageSize);
            }
        }
    }

}
